# delimit ;
clear ;
set more off ;
version 12.0 ;

cd "replication" ;

* ***************************************************************************** ;
* Carpena, Fenella, and Bilal Zia. "The causal mechanism of financial education: 
* Evidence from mediation analysis." Journal of Economic Behavior & Organization 
* 177 (2020): 143-184.
* 
* This do-file creates Appendix Table A7 of the paper
* ***************************************************************************** ;

* ***************************************************************************** ;
* get endline data
* ***************************************************************************** ;

use "./data/endline.dta", clear ;

* merge with baseline ;
merge 1:1 id using "./data/baseline.dta" ;
tab _merge ;

* correction to the variable label ;
label var savings_bank "has bank savings account" ;

* ***************************************************************************** ;
* sample selection
* ***************************************************************************** ;

* drop households with baseline but no endline ;
drop if _merge == 2 ; 
drop _merge ;

* drop households with no financial knowledge scores ; 
egen temp_knowl = rowmean(numer_fin_return-attit_budget) ;
drop if temp_knowl == . ;
drop temp_knowl ; 

* identify respondents with endline measures of numeracy ;
merge 1:1 id using "./data/financial-knowledge-short-term", keepusing(id) ;
tab _merge ;
keep if _merge == 3 ;
drop _merge ;

* drop households with missing discount rate (control variable) ;
drop if disc_rate == . ;

* ***************************************************************************** ;
* create financial knowledge scores
* ***************************************************************************** ;

egen end_numeracy = rowmean(numer_fin_return-numer_int_rate) ;
egen end_awareness = rowmean(aware_budget-aware_unprod) ;
egen end_attitudes = rowmean(attit_suggest_ins-attit_budget) ;

* ***************************************************************************** ;
* create treatment dummies
* ***************************************************************************** ;

gen flcounsgoal = (fin_ed_treatment == 1 & couns_treatment == 1 & goal_treatment == 1)  ;
gen flcouns = (fin_ed_treatment == 1 & couns_treatment == 1 & goal_treatment == 0) ;
gen flgoal = (fin_ed_treatment == 1 & couns_treatment == 0 & goal_treatment == 1) ;
gen fl = (fin_ed_treatment == 1 & couns_treatment == 0 & goal_treatment == 0) ;
gen control = (fin_ed_treatment == 0 & couns_treatment == 0 & goal_treatment == 0) ;

* ***************************************************************************** ;
* control variables
* ***************************************************************************** ;

local cvars has_hard_time_saving interested_in_financial inconsistent disc_rate riskaverse  ;
local rcvars rhas_hard_time_saving rinterested_in_financial rinconsistent rdisc_rate rriskaverse ;

* ***************************************************************************** ;
* rename variables to make them shorter ;
* ***************************************************************************** ;

ren budget_helpful bdg_help ;
ren budget_tried bdg_tried ;
ren budget_regular bdg_reg ;
ren savings_informal sav_inf ;
ren savings_bank sav_bnk ;
ren loan_purpose_bus_educ loan_bus ; 
ren ins_life ins_life ;

* ***************************************************************************** ;
* make output file
* ***************************************************************************** ;

shell printf "********** Appendix Table A7 ********** \r\n" > "./output/appendix-table-A07.csv" ;
shell printf "Treatment, Outcome, Mediator, Delta\n" >> "./output/appendix-table-A07.csv" ;

* ***************************************************************************** ;
* APPENDIX TABLE A7, PANEL A: FL only 
* ***************************************************************************** ;

shell printf "********** Panel A ********** \r\n" >> "./output/appendix-table-A07.csv" ;

local t fl ;
local outcomes bdg_tried ; 

* loop over all outcome variables and mediators ;
foreach y of local outcomes { ;
	
	foreach m of varlist end_numeracy end_awareness end_attitudes { ;

		di "*********** outcome var: `y', mediator: `mediator' ******************* " ;
		qui areg `y' `t' `m' `cvars', a(strata) clu(wave_class) ;
		psacalc delta `m', beta(0) rmax(1)  ;
		local delta = round(r(delta), 0.001) ;
		
		shell printf "`t', `y', `m', `delta', \n" >> "./output/appendix-table-A07.csv" ;
				
	} ;
} ;

* ***************************************************************************** ;
* APPENDIX TABLE A7, PANEL B: FL + goal
* ***************************************************************************** ;

shell printf "********** Panel B ********** \r\n" >> "./output/appendix-table-A07.csv" ;

local t flgoal ;
local outcomes bdg_tried sav_bnk ; 

* loop over all outcome variables and mediators ;
foreach y of local outcomes { ;
	
	foreach m of varlist end_numeracy end_awareness end_attitudes { ;

		di "*********** outcome var: `y', mediator: `mediator' ******************* " ;
		qui areg `y' `t' `m' `cvars', a(strata) clu(wave_class) ;
		psacalc delta `m', beta(0) rmax(1)  ;
		local delta = round(r(delta), 0.001) ;
		
		shell printf "`t', `y', `m', `delta', \n" >> "./output/appendix-table-A07.csv" ;
				
	} ;
} ;

* ***************************************************************************** ;
* APPENDIX TABLE A7, PANEL C: FL + couns
* ***************************************************************************** ;

shell printf "********** Panel C ********** \r\n" >> "./output/appendix-table-A07.csv" ;

local t flcouns ;
local outcomes bdg_tried sav_bnk loan_bus ins_life ; 

* loop over all outcome variables and mediators ;
foreach y of local outcomes { ;
	
	foreach m of varlist end_numeracy end_awareness end_attitudes { ;

		di "*********** outcome var: `y', mediator: `mediator' ******************* " ;
		qui areg `y' `t' `m' `cvars', a(strata) clu(wave_class) ;
		psacalc delta `m', beta(0) rmax(1)  ;
		local delta = round(r(delta), 0.001) ;
		
		shell printf "`t', `y', `m', `delta', \n" >> "./output/appendix-table-A07.csv" ;
				
	} ;
} ;

* ***************************************************************************** ;
* APPENDIX TABLE A7, PANEL D: FL + couns + goal
* ***************************************************************************** ;

shell printf "********** Panel D ********** \r\n" >> "./output/appendix-table-A07.csv" ;

local t flcounsgoal ;
local outcomes bdg_tried sav_bnk ins_life ; 

* loop over all outcome variables and mediators ;
foreach y of local outcomes { ;
	
	foreach m of varlist end_numeracy end_awareness end_attitudes { ;

		di "*********** outcome var: `y', mediator: `mediator' ******************* " ;
		qui areg `y' `t' `m' `cvars', a(strata) clu(wave_class) ;
		psacalc delta `m', beta(0) rmax(1)  ;
		local delta = round(r(delta), 0.001) ;
		
		shell printf "`t', `y', `m', `delta', \n" >> "./output/appendix-table-A07.csv" ;
				
	} ;
} ;

exit ;
